package com.face.controller;

import com.face.common.Result;
import com.face.service.RemarkService;
import com.face.service.UserService;
import com.face.wrapper.RemarkWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private RemarkService remarkService;

    @Autowired
    private UserService userService;

    @GetMapping
    public Result getData(@RequestParam("shopId") String shopId) {
        Integer allUserCount = userService.count(shopId);
        Integer allRemarkCount = remarkService.count(shopId);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6);
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String start = format.format(date);
        Map<String, Object> chartData = remarkService.getChartData(shopId, start, format.format(new Date()), 1);
        Map<String, Object> remarkCountData = remarkService.getRemarkCountData(shopId, start, format.format(new Date()), 1);
        long[] remarkCountList = (long[]) remarkCountData.get("remarkCount");
        chartData.put("remarkCount", remarkCountList);
        Integer newRemarkCount = remarkService.count(shopId, format.format(new Date()), format.format(new Date()));

        List<RemarkWrapper> list = remarkService.getRecentRemarksByShopId(shopId, 5);
        Map result = new HashMap();
        result.put("allUserCount", allUserCount);
        result.put("allRemarkCount", allRemarkCount);
        result.put("chartData", chartData);
        result.put("newRemarkCount", newRemarkCount);
        result.put("recentRemarks", list);

        return Result.success(result);
    }
}
